/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package onlineregistration;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TQNINH
 */
public class Member {
    
    public Member() {
        
    }
    
    public boolean validateEmail(String emailID) {
        boolean flgDotPosition = false;
        boolean flgFound = false;
        boolean flgDot = false;
        boolean flgIllegalChar = false;
        
        Pattern patEmail = Pattern.compile("^\\.|^\\@");
        Matcher match = patEmail.matcher(emailID);
        
        if(match.find()) {
            flgIllegalChar = true;
            System.out.println("Email addresses dont start with dots or @ sign");
        }
        
        int position = emailID.indexOf("@");
        int dotPosition = emailID.indexOf(".");
        
        if(position == -1) {
            flgFound = true;
            System.out.println("Email address does not have @ sign.");
        }
        
        if(dotPosition == -1) {
            flgDot = true;
            System.out.println("Email does not contain a dot.");
        }
        
        patEmail = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
        match = patEmail.matcher(emailID);
        StringBuffer strBuffer = new StringBuffer();
        boolean result = match.find();
        boolean deletedIlleagalChars = false;
        while(result) {
            deletedIlleagalChars = true;
            match.appendReplacement(strBuffer, "");
            result = match.find();
        }
        
        match.appendTail(strBuffer);
        if(!(deletedIlleagalChars || flgIllegalChar || flgDotPosition
                || flgFound || flgDot)) {
            System.out.println("Valid E-mail address!");
            return true;
        } else {
            System.out.println("Invalid E-mail address!");
            return false;
        }
    }
    
    public boolean validatePassword(String pass) {
        boolean upperCase = false;
        boolean number = false;
        boolean illegalChars = false;
        
        Pattern patPassword = Pattern.compile("[A-Z]");
        Matcher matcher = patPassword.matcher(pass);
        
        if(matcher.find()) {
            upperCase = true;
        } else {
            System.out.println("Password must contain atleast one upper case letter!");
        }
        
        patPassword = Pattern.compile("[\\d]");
        matcher = patPassword.matcher(pass);
        
        if(matcher.find()) {
            number = true;
        } else {
            System.out.println("Password must contain atleast one number!");
        }
        
        patPassword = Pattern.compile("[^A-Za-z0-9]");
        matcher = patPassword.matcher(pass);
        
        if(matcher.find()) {
            illegalChars = true;
            System.out.println("Password cannot contain special characters!");
        }
        
        if(upperCase && number && (!illegalChars)) {
            return true;
        } else {
            return false;
        }
    }
    
}
